﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NRemedy.CodeGenerator.WinForm
{
    public partial class Main : Form
    {

        private GetFormsThread _getFormsThread;
        private SearchFormThread _searchFormThread;
        private CodeGenerateThread _codeGenerateThread;

        public Main()
        {
            InitializeComponent();
            SetUI();
            InitObject();
        }

        private void SetUI()
        {
            this.txtFilterValue.KeyDown += new KeyEventHandler(txtFilterValue_KeyDown);
            this.tvwFormList.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(tvwFormList_NodeMouseDoubleClick);
            this.mitemCopyAll.Click += new EventHandler(mitemCopyAll_Click);
        }

        private void mitemCopyAll_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(txtCodeArea.Text, true);
        }

        private void tvwFormList_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (!_codeGenerateThread.IsRunning)
            {
                _codeGenerateThread.Start(e.Node.Text);
            }
        }

        private void InitObject()
        {
            _getFormsThread = new GetFormsThread();
            _getFormsThread.GetFormsCompeleted = LoginSuccess;
            _searchFormThread = new SearchFormThread();
            _searchFormThread.SearchCompeleted += new PushFormListDelegate(_searchFormThread_SearchCompeleted);
            _codeGenerateThread = new CodeGenerateThread();
            _codeGenerateThread.GenerateCompeleted += new PushCodeDelegate(_codeGenerateThread_GenerateCompeleted);

        }

        private void LoginSuccess(bool isSuccess, string exceptionMessage)
        {
            if (!isSuccess)
            {
                MessageBox.Show(exceptionMessage);
            }
            else
            {
                info.Text = " Finish Read Form Cache "+DateTime.Now.ToString();
            }
        }

        private void _searchFormThread_SearchCompeleted(IEnumerable<ARFormDisplay> formList)
        {
            tvwFormList.Nodes.Clear();
            foreach (var f in formList)
            {
                TreeNode tn = new TreeNode(f.Name);
                tvwFormList.Nodes.Add(tn);
            }
        }


        private void _codeGenerateThread_GenerateCompeleted(string code)
        {
            this.txtCodeArea.Text = code;
        }

        private void txtFilterValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (_getFormsThread.IsRunning)
                {
                    MessageBox.Show("Reading the form cache. please wait a minute");
                    return;
                }

                if (txtFilterValue.Text.Length >= 5)
                {
                    if (!_searchFormThread.IsRunning)
                    {
                        _searchFormThread.Start(txtFilterValue.Text);
                    }
                    else
                    {
                        MessageBox.Show("Searching  ");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("at least 5 char");
                }
            }
        }

        private void login_Click(object sender, EventArgs e)
        {

            Global.FormCache = null;
            string server = text_Server.Text;
            string user = text_User.Text;
            string password = text_Password.Text;
            string authentication = text_Authentication.Text;
            if (string.IsNullOrWhiteSpace(server) || string.IsNullOrWhiteSpace(user)
                || string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("Parameter(Server,User,Password) Is All Necessary");
                return;
            }
            _getFormsThread.Start(server, user, password, authentication);

            string[] foodList = new string[]{"Juice", "Coffee", 
			"Cereal & Milk", "Fruit Plate", "Toast & Jelly", 
			"Bagel & Cream Cheese"};
            string[] foodPrice = new string[]{"1.09", "1.09", "2.19", 
			"2.49", "1.49", "1.49"};

            for (int count = 0; count < foodList.Length; count++)
            {
                ListViewItem listItem = new ListViewItem(foodList[count]);
                listItem.SubItems.Add(foodPrice[count]);
                list_Fields.Items.Add(listItem);
            }
        }

        private void Generate_Click(object sender, EventArgs e)
        {
            if (_getFormsThread.IsRunning)
            {
                MessageBox.Show("Reading the form cache. please wait a minute");
                return;
            }
            if (_searchFormThread.IsRunning)
            {
                MessageBox.Show("Searching  ");
                return;
            }
            if (!_codeGenerateThread.IsRunning)
            {
                //do somehing.
            }
            else
            {
                MessageBox.Show("Generating   ");
                return;
            }
        }

        private void list_Fields_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            var item = e.Item;
            if (item.Checked)
                MessageBox.Show(item.SubItems[0].Text);
        }

        //保留Form 名称和 所有Field,当选定时标注Filed 为已选择。 进而产生

    }
}
